package org.lt.cj.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class SingleProperty {

    private Properties prop = null;
    private InputStream in = null;
    private OutputStream os;
    private String path;

    public SingleProperty(String path) {
        this.path = path;
        initProperty();
    }

    private void initProperty() {
        try {
            prop = new Properties();
            in = new FileInputStream(this.path); //"src/org/lt/cj/config/config.properties"
        } catch (FileNotFoundException ex) {
        }
    }

    private void setIs() {
        try {
            if (in == null) {
                in = new FileInputStream(this.path);
            }
        } catch (IOException ex) {
        }
    }

    private void setOs() {
        try {
            os = new FileOutputStream(this.path); 
        } catch (FileNotFoundException ex) {
        }
    }

    public Properties getProperty() {
        try {
            setIs();
            prop.load(in);
        } catch (IOException ex) {
        }
        return prop;
    }

    public String getValue(String key) {
        String value = "";
        try {
            setIs();
            prop.load(in);
            value = prop.getProperty(key);
        } catch (IOException ex) {
        }
        return value;
    }

    public void addProperty(Map<String, String> map, String comments) {
        try {
            setIs();
            prop.load(in);
            setOs();

            Set keys = map.keySet();
            for (Iterator it = keys.iterator(); it.hasNext();) {
                String key = (String) it.next();
                String value = map.get(key);
                prop.setProperty(key, value);
            }
            prop.store(os, comments);
        } catch (IOException ex) {
        }
    }

    public void setProperty(String key, String value){
        prop.setProperty(key, value);
    }

    public void store(String comments){
        try {
            setOs();
            prop.store(os, comments);
        } catch (IOException ex) {
        }
    }

    public void deleteProperty(String key, String comments) {
        try {
            setIs();
            prop.load(in);
            prop.remove(key);
            setOs();
            prop.store(os, comments);
        } catch (IOException ex) {
        }
    }

    public void endAndcloseIO() {
        try {
            if (os != null) {
                os.flush();
                os.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
        }
    }

    public void printMap(Map map) {
        Set keys = map.keySet();
        for (Iterator it = keys.iterator(); it.hasNext();) {
            Object key = it.next();
            Object value = map.get(key);
            System.out.println(key + "==" + value);
        }
    }

    public static void main(String[] args) throws Exception {
        SingleProperty p = new SingleProperty("src/org/lt/cj/config/config.properties");
        Map<String, String> map = new HashMap<String, String>();
        map.put("taskName", "upload");
        map.put("enterURLs", "http://zhidao.baidu.com/");
        map.put("fitURLs", "http://zhidao.baidu.com/");
        map.put("pageEncoding", "gbk");
        map.put("workingThreads", "6");
        map.put("regularRules", "<div class=\"utable\">");
        map.put("qustion", "<div class=\"qtable\">");
        map.put("bestAnswer", "<div class=\"bestAnswer\">");
        map.put("candidateAnswer", "<div class=\"other\">");
        p.addProperty(map, "修改配置参数");


        //p.addProperty("name", "ahome", "增加配置参数age");
        //p.deleteProperty("age","删除配置参数template");
        //String value = p.getValue("name");
        //System.out.println("main.value=" + value);
        Map map1 = p.getProperty();
        p.printMap(map1);
        String reg = p.getValue("regularRules");
        String regular[] = reg.split("@");
        for (int i = 0; i < regular.length; i++) {
            System.out.println("regular[" + i + "]=" + regular[i]);
        }
        //同一任务的多项用&分隔，任务间用@分隔，
    }
}
